﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class user_QueryOrder : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        SqlConnection sqlcon;
        SqlCommand sqlcom;
        //行的状态是： 编辑状态 或者 （交替行且是编辑状态）
        if ((e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
        {
            Response.Write("11111");
            String strConnection = "Server=.;Database=keshe2;Integrated Security=SSPI";
            SqlConnection con = new SqlConnection(strConnection);
            con.Open();
            String sql = "SELECT * from 订单 where 订单编号 = " + GridView1.DataKeyNames;
            sqlcon = new SqlConnection(strConnection);
            sqlcon.Open();
            sqlcom = new SqlCommand(sql, sqlcon);
            SqlDataReader Dr = sqlcom.ExecuteReader();
            int a = 0;
            string sqlstr = "";
            try
            {
                while (Dr.Read())
                {
                    if (Dr["收货状态"].ToString() == "1")
                    {
                        sqlstr = "update 订单 set 收货状态=1 where 订单编号=" + GridView1.DataKeyNames;

                    }

                }
            }
            catch (SqlException ex)
            {
                Response.Write(ex.Message.ToString());
            }
            Dr.Close();
            sqlcon.Close();
            sqlcon = new SqlConnection(strConnection);
            sqlcom = new SqlCommand(sqlstr, sqlcon);
            sqlcon.Open();
            sqlcom.ExecuteNonQuery();
            sqlcon.Close();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string name = TextBox1.Text;
        if (name.Length != 0)
        {
            SqlConnection sqlcon;
            SqlCommand sqlcom;

            String strConnection = "Server=.;Database=keshe2;Integrated Security=SSPI";
            SqlConnection con = new SqlConnection(strConnection);
            con.Open();
            String sql = "SELECT 订单.订单编号, 订单.客户编号, 商品.商品名称, 订单.数量, 订单.总价, 订单.创建时间, 订单.订单状态 FROM 订单 INNER JOIN 商品 ON 订单.商品编号 = 商品.商品编号 WHERE (订单.客户编号 = @客户编号)";
            sqlcon = new SqlConnection(strConnection);
            sqlcon.Open();
            sqlcom = new SqlCommand(sql, sqlcon);
            SqlDataReader Dr = sqlcom.ExecuteReader();
            int a = 0;
            try
            {
                while (Dr.Read())
                {
                    string name1 = Dr["商品名称"].ToString();
                    if (name1.Contains(name))
                    {
                        string sql1 = "SELECT 订单.订单编号, 订单.客户编号, 商品.商品名称, 订单.数量, 订单.总价, 订单.创建时间, 订单.订单状态 FROM 订单 INNER JOIN 商品 ON 订单.商品编号 = 商品.商品编号 WHERE (订单.客户编号 = @客户编号) where 商品名称 like '%" + name + "%'";
                        GridView1.DataSourceID = "SqlDataSource1";
                        SqlDataSource1.SelectCommand = sql1;
                        a = 1;
                        break;
                    }

                }
                if (a == 0)
                {
                    GridView1_empty();
                }
            }
            catch (SqlException ex)
            {
                Response.Write(ex.Message.ToString());
            }
            finally
            {
                Dr.Close();
            }
            sqlcom.ExecuteNonQuery();
            sqlcon.Close();


        }
    }
    protected void GridView1_empty()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("商品名称");
        dt.Columns.Add("数量");
        dt.Columns.Add("总价");
        dt.Columns.Add("创建时间");
        dt.Columns.Add("订单状态");
        if (dt.Rows.Count == 0)
        {
            dt.Rows.Add(dt.NewRow());
            GridView1.DataSourceID = "";
            GridView1.DataSource = dt;
            GridView1.DataBind();
            int columnCount = dt.Columns.Count;
            GridView1.Rows[0].Cells.Clear();
            GridView1.Rows[0].Cells.Add(new TableCell());
            GridView1.Rows[0].Cells[0].ColumnSpan = columnCount;
            GridView1.Rows[0].Cells[0].Text = "暂无该商品记录！";
            GridView1.Rows[0].Cells[0].Style.Add("text-align", "center");
        }

    }


    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.Url.ToString());
    }
}